package com.example.compose

import android.os.Bundle
import android.view.KeyEvent
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.offset
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Done
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.draw.rotate
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.compose.ui.theme.ComposeTheme
import com.example.compose.ui.theme.StatusBarColor
import com.google.accompanist.web.WebView
import com.google.accompanist.web.rememberWebViewState

class MainActivity3 : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        WindowCompat.setDecorFitsSystemWindows(window, false)
        window.statusBarColor = StatusBarColor
        val url = intent.getStringExtra("url") ?: "http://m.xacazj.com"
        val javaScript = intent.getBooleanExtra("javaScript", false)
        var interesting = 0
        setContent {
            ComposeTheme {
                val top = remember { mutableStateOf(url)}
                val webViewState = rememberWebViewState(top.value)
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background
                ) {
                    Box() {
                        WebView(
                            state = webViewState,
                            onCreated = { it.settings.javaScriptEnabled = javaScript },
                            captureBackPresses = false
                        )
                        FloatingActionButton(
                            modifier = Modifier.offset(0.dp,300.dp).rotate(270f).alpha(0.1f),
                            onClick = {
                                var index = url.subSequence(url.length - 9,url.length - 5).toString().toInt()
                                top.value = "${url.subSequence(0,url.length - 9)}${index+interesting++}.html"
                            }
                        ) {
                            Icon(Icons.Outlined.Done, contentDescription = "Localized description" )
                        }
                    }

                }
            }
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
//        if (keyCode==KeyEvent.KEYCODE_BACK){
//            return true
//        }
        return super.onKeyDown(keyCode, event)
    }
}

@Composable
fun Greeting(name: String) {
    Text(text = "Hello $name!")
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    ComposeTheme {
        Greeting("Android")
    }
}